package com.chenxys.algorithm.newbie;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *      链表反转：将链表原来为头的节点变成尾 单链表
 * </p>
 *
 * @author Chenxys
 * @version V1.0
 * @Package com.chenxys.algorithm.newbie
 * @date 12/27/23 11:13 PM
 * @Copyright ©
 */
@SuppressWarnings("all")
public class ReverseSingleList {

    /**
     * <p>
     *     单链表数据结构
     * </p>
     */
    public static class Node{
        // 当前值
        public int value;
        // 下一个节点
        public Node next;

        public Node(int value) {
            this.value = value;
        }
    }

    /**
     * <p>
     *     反转链表，并返回反转后的头节点
     * </p>
     * @param head 链表
     * @return 反转后的链表
     */
    public static Node reverseSingleList(Node head){
        Node pre = null;
        Node next = null;
        while (head != null){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }


    /**
     * <p>
     *     测试单向链表反转方法是否正确
     * </p>
     * @param head 待反转单向链表
     * @return 反转后链表
     */
    public static Node testReverseSingleList(Node head){
        if (head == null) return null;
        ArrayList<Node> list = new ArrayList<>();
        while (head != null) {
            list.add(head);
            head = head.next;
        }
        list.get(0).next = null;
        int N = list.size();
        for (int i = 0; i < N; i++) {
            list.get(i).next = list.get(i - 1);
        }
        return list.get(N - 1);
    }


    /**
     * <p>
     *     生成随机单向链表
     * </p>
     * @param len 链表长度
     * @param value 链表最大值
     * @return 随机单向链表
     */
    public static Node generateRandomSingleList(int len, int value) {
        int size = (int) (Math.random() * (len + 1));
        if (size == 0) return null;
        
        size--;
        Node head = new Node((int) (Math.random() * (value + 1)));
        Node pre = head;
        while (size != 0){
            Node cur = new Node((int) (Math.random() * (value + 1)));
            pre.next = cur;
            pre = cur;
            size--;
        }
        return head;
    }

    /**
     * <p>
     *     获取单向链表原有顺序
     * </p>
     * @param head 单向链表
     * @return 记录元素顺序
     */
    public static List<Integer> getSingleListOriginOrder(Node head) {
        List<Integer> ans = new ArrayList<>();
        while (head != null) {
            ans.add(head.value);
            head = head.next;
        }
        return ans;
    }

    /**
     * <p>
     *     检查反转后的顺序是否与原顺序相反
     * </p>
     * @param origin 原链表顺序
     * @param head 反转后链表
     * @return 是否相反
     */
    public static boolean checkSingleListReverse(List<Integer> origin, Node head) {
        for (int i = origin.size() - 1; i >= 0; i--) {
            if (!origin.get(i).equals(head.value)){
                return false;
            }
            head = head.next;
        }
        return true;
    }


    public static void main(String[] args) {
        int len = 50;
        int value = 100;
        int testTimes = 100000;
        System.out.println("Test Begin!");
        for (int i = 0; i < testTimes; i++) {
            Node node1 = generateRandomSingleList(len, value);
            List<Integer> list1 = getSingleListOriginOrder(node1);
            node1 = reverseSingleList(node1);
            if (!checkSingleListReverse(list1, node1)) {
                System.out.println("Oops1!");
            }

            Node node2 = generateRandomSingleList(len, value);
            List<Integer> list2 = getSingleListOriginOrder(node2);
            node2 = reverseSingleList(node2);
            if (!checkSingleListReverse(list2, node2)) {
                System.out.println("Oops2!");
            }
        }
        System.out.println("Test Finish!");
    }
}
